<template>
  <rx-dialog @handOk="handOk" @cancel="cancel">
    <template #toolbar>
      <a-checkbox :checked="checkStrictly" @change="changeCheckStrictly(checkStrictly)">
        {{i18n('cascadeSelect','级联选择')}}
      </a-checkbox>
      <a-checkbox :checked="checkAll" @change="onChange">
        {{$ti18('rxList.selectAll','全选')}}
      </a-checkbox>
    </template>
  <a-tree
    checkable
    :multiple="!isSingle"
    v-model="checkedKeys"
    :show-icon="false"
    :treeData="treeData"
    :checkStrictly="!checkStrictly"
    @check="checkTree"
    @select="selectTree"
  ></a-tree>
  </rx-dialog>
</template>

<script>
    import SysTreeApi from "@/api/system/core/sysTree";
    import {Util,App} from "@lowcode/jpaas-common-lib"
    export default {
      name: "SysTreeDialog",
      mixins:[App],
      props: {
        catKey: {
          type: String,
          default: ''
        },
        selectedKeys:{
          type: Array,
          default:()=>[]
        },
        isSingle: {
          type: Boolean,
          default: false
        },
        layerid: {
          type: String,
          default: ""
        },
        destroy: {
          type: Function
        }
      },
      data(){
        return {
          checkStrictly:false,
          treeData:[],
          allTreeList:[],
          selectedRows:[],
          checkedKeys:[],
          checkAll: false,
        }
      },
      created(){
        this.checkedKeys.push(...this.selectedKeys);
        this.loadData();
      },
      methods:{
          i18n(name,text,key){
              return this.$ti18(name,text,"SysTree",key);
          },
        changeCheckStrictly(value) {
          this.checkStrictly = !value;
        },
        onChange(e) {
          this.checkAll = e.target.checked;
          const {allTreeList} = this;
          this.checkedKeys = [];
          if (this.checkAll) {
            for (let i = 0; i < allTreeList.length; i++) {
              this.checkedKeys.push(allTreeList[i].treeId);
              this.selectedRows.push(allTreeList[i]);
            }
          }
        },
        loadData(){
          SysTreeApi.getByCatKey(this.catKey,this.curApp.appId).then(res=>{
            this.allTreeList=res;
            this.selectedRows=res.filter(item=>this.checkedKeys.includes(item.treeId));
            var ary=Util.listToTree(res,"treeId","parentId");
            Util.genTreeData(ary,"name","treeId","treeId");
            this.treeData=ary;
          })
        },
        checkTree(selKeys, e){
          this.selectedRows=[];
          for(var i=0;i<e.checkedNodes.length;i++){
              var obj = e.checkedNodes[i];
              this.selectedRows.push(obj.data.props);
          }
        },
        selectTree(selKeys, e){
          if(!this.checkedKeys.includes(selKeys[0])) {
              this.checkedKeys.push(...selKeys);
              this.selectedRows.push(e.selectedNodes[0].data.props);
          }else{
              var idx=this.checkedKeys.indexOf(selKeys[0]);
              this.checkedKeys.splice(idx,1);
              this.selectedRows.splice(idx,1);
          }
        },
        getData(){
          return this.selectedRows;
        },
        cancel(){
          Util.closeWindow(this,"cancel");
        },
        handOk(e){
          var rows=this.selectedRows;
          if(rows.length==0){
              this.$message.warning(this.i18n('msa.selectedRowsTips','请选择一行记录!'));
              e.loading=false;
              return;
          }
          Util.closeWindow(this,"ok");
        }
      }
    }
</script>

<style scoped>

</style>